package com.lagou.edu.service.impl;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.annotation.Transation;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.dao.impl.JdbcAccountDaoImpl;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;

/**
 * @author 应癫
 */
@Service("transferService")
public class TransferServiceImpl implements TransferService {


    // 最佳状态
    @Autowired("accountDao")
    private JdbcAccountDaoImpl jdbcAccountDaoImpl;

    // 构造函数传值/set方法传值




    @Override
    @Transation
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        /*try{
            // 开启事务(关闭事务的自动提交)
            TransactionManager.getInstance().beginTransaction();*/

    Account from = jdbcAccountDaoImpl.queryAccountByCardNo(fromCardNo);
    Account to = jdbcAccountDaoImpl.queryAccountByCardNo(toCardNo);

    from.setMoney(from.getMoney() - money);
    to.setMoney(to.getMoney() + money);

        jdbcAccountDaoImpl.updateAccountByCardNo(to);

        jdbcAccountDaoImpl.updateAccountByCardNo(from);


        /*    // 提交事务

            TransactionManager.getInstance().commit();
        }catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            TransactionManager.getInstance().rollback();

            // 抛出异常便于上层servlet捕获
            throw e;

        }*/




    }
}
